package com.tianxin.workbao.workBao.Activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.FragmentStareAdapter;
import com.tianxin.workbao.workBao.fragment.WashWorkFragment;

/**
 * Created by hacker on 2017/6/22.
 */

public class WashWorkActivity extends BaseActivity {

    private ViewPager mViewPager;
    private FloatingActionButton mFab;
    private FragmentStareAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initStatusBar(true);
        setContentView(R.layout.common_main);

        initData();
        initView();
        initEvent();
        bindView();
    }

    public void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle("洗煤班次");
        mToolbar.setNavigationIcon(R.drawable.ic_action_back_24dp);
        setSupportActionBar(mToolbar);

        mViewPager = findViewById(R.id.viewPager);
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setCurrentItem(0);

        mFab = findViewById(R.id.top_fab);
    }

    @Override
    public void initData() {
        mFragments.add(0, WashWorkFragment.newInstance());

    }

    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(view -> WashWorkActivity.this.finish());
        // fab 刷新
        mFab.setOnClickListener(view -> deepRefresh());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.add_share_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add:
                startActivity(new Intent(WashWorkActivity.this, WashWorkOptionActivity.class));
                break;
            case R.id.action_Share:
                Toast.makeText(this, R.string.developing, Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 深度刷新
     */
    private void deepRefresh() {
        updateUi();
    }

    private void updateUi() {
        mFragments.get(0).initData(false);
    }

    @Override
    public void bindView() {
        mAdapter = new FragmentStareAdapter(getSupportFragmentManager(), mFragments, null);
        mViewPager.setAdapter(mAdapter);
    }

    @Override
    protected void onRestart() {
        updateUi();
        super.onRestart();
    }
}
